﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using System.Collections.Generic;

public partial class System_PermissionNew : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCategoryList();
            if (Request.QueryString["PermissionID"] == null)
            {
                ActionTitle.Text = "添加权限";
            }
            else
            {
                ActionTitle.Text = "编辑权限";
                BindData();
                PermissionID.ReadOnly = true;
            }
        }
    }
    private void BindCategoryList()
    {
        List<PermissionCategoryInfo> categories = PermissionCategory.GetList("");
        ListItem li;
        foreach (PermissionCategoryInfo category in categories)
        {
            li = new ListItem();
            li.Text = category.CategoryName;
            li.Value = category.CategoryID;
            CategoryList.Items.Add(li);
        }
    }
    private void BindData()
    {
        PermissionInfo permission = Yipusoft.Account.BLL.Permission.GetPermission(Request.QueryString["PermissionID"]);
        PermissionID.Text = permission.PermissionID;
        CategoryList.SelectedIndex = CategoryList.Items.IndexOf(CategoryList.Items.FindByValue(permission.CategoryID));
        PermissionName.Text = permission.PermissionName;
        Description.Text = permission.Description;
    }
    private void Insert()
    {
        if (Yipusoft.Account.BLL.Permission.Exsits(PermissionID.Text))
        {
            Prompt.Text = "警告：该权限编码已存在！";
            return;
        }
        PermissionInfo permission = new PermissionInfo();
        permission.PermissionID = PermissionID.Text;
        permission.CategoryID = CategoryList.SelectedValue;
        permission.PermissionName = PermissionName.Text;
        permission.Description = Description.Text;
        Yipusoft.Account.BLL.Permission.Insert(permission);
        Prompt.Text = "提示：添加成功！";
    }
    private void Update()
    {
        PermissionInfo permission = Yipusoft.Account.BLL.Permission.GetPermission(Request.QueryString["PermissionID"]);
        permission.CategoryID = CategoryList.SelectedValue;
        permission.PermissionName = PermissionName.Text;
        permission.Description = Description.Text;
        Yipusoft.Account.BLL.Permission.Update(permission);
        Prompt.Text = "提示：更新成功！";
    }
    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["PermissionID"] == null)
            Insert();
        else
            Update();
    }
}
